package com.neusoft.neuiotms.dcom.converter;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.FunctionalGroupDTO;
import com.neusoft.neuiotms.dcom.model.FunctionalGroup;
import com.neusoft.neuiotms.dcom.model.FunctionalUser;
import com.neusoft.neuiotms.dcom.model.HandoverItem;
import com.neusoft.neuiotms.dcom.repository.FunctionalGroupRepository;
import com.neusoft.neuiotms.dcom.repository.FunctionalUserRepository;
import com.neusoft.neuiotms.dcom.repository.HandoverItemRepository;

@Component
public class FunctionalGroupConverter extends AbstractConverter<FunctionalGroup, FunctionalGroupDTO> {

    @Autowired
    private FunctionalGroupRepository functionalGroupRepository;
    @Autowired
    private FunctionalUserConverter functionalUserConverter;
    @Autowired
    private FunctionalUserRepository functionalUserRepository;
    @Autowired
    private HandoverItemConverter handoverItemConverter;
    @Autowired
    private HandoverItemRepository handoverItemRepository;

    @Override
    public FunctionalGroup toModel(final FunctionalGroupDTO dto) {

        FunctionalGroup model;

        if (dto.isNew()) {
            model = new FunctionalGroup();
        } else {
            model = this.functionalGroupRepository.getOne(dto.getId());
        }

        model.setName(dto.getName());
        model.setOrgCode(dto.getOrgCode());
        model.setOrgName(dto.getOrgName());

        final List<FunctionalUser> users = model.getUsers().stream().collect(Collectors.toList());

        users.forEach(k -> {
            model.getUsers().remove(k);
            this.functionalUserRepository.delete(k);
        });

        dto.getUsers().stream().map(s -> {
            final FunctionalUser userModel = this.functionalUserConverter.toModel(s);
            userModel.setGroup(model);
            return userModel;
        }).forEach(model.getUsers()::add);

        final List<HandoverItem> items = model.getItems().stream().collect(Collectors.toList());

        items.forEach(k -> {
            model.getItems().remove(k);
            this.handoverItemRepository.delete(k);
        });

        dto.getItems().stream().map(s -> {
            final HandoverItem itemModel = this.handoverItemConverter.toModel(s);
            itemModel.setGroup(model);
            return itemModel;
        }).forEach(model.getItems()::add);

        return model;
    }

    @Override
    public FunctionalGroupDTO toDTO(final FunctionalGroup model, final boolean forListView) {

        final FunctionalGroupDTO dto = new FunctionalGroupDTO();

        dto.setId(model.getId());
        dto.setName(model.getName());
        dto.setOrgCode(model.getOrgCode());
        dto.setOrgName(model.getOrgName());

        model.getUsers().stream().map(s -> this.functionalUserConverter.toDTO(s)).filter(k -> null != k)
                .forEach(dto.getUsers()::add);

        if (!forListView) {
            model.getItems().stream().map(s -> this.handoverItemConverter.toDTO(s)).filter(k -> null != k)
                    .forEach(dto.getItems()::add);
        }
        return dto;
    }

}
